import { Body, Controller, Get, Param, Patch, Post } from '@nestjs/common';
import { TasksService } from './tasks.service.js';

@Controller('tasks')
export class TasksController {
  constructor(private readonly tasks: TasksService) {}

  @Get()
  async list() {
    return this.tasks.list();
  }

  @Post()
  async create(@Body() body: { title: string; description?: string }) {
    if (!body?.title || body.title.trim().length === 0) {
      return { error: 'title is required' } as const;
    }
    return this.tasks.create({ title: body.title.trim(), description: body.description });
  }

  @Patch(':id/done')
  async done(@Param('id') id: string) {
    return this.tasks.setDone(id);
  }
}
